package com.neuedu.mblog.dao;

import com.neuedu.mblog.entiry.Tag;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
* 项目：his
* 创建时间：  2021-12-28 15:47:17
* 作者 :金山老师
* 描述 : 数据访问层
*/
public class TagDao extends BaseDao<Tag> {

    private QueryRunner run = new QueryRunner( dataSource );

    @Override
    public List<Tag> selectList(String sql ) throws Exception {
        throw  new Exception("暂未实现的方法");
    }


    /**
     * 批量查询
     * @param sql
     * @param params
     * @return
     * @throws SQLException
     */
    @Override
    public List<Tag> selectList(String sql, Object ... params ) throws SQLException {

        //每张表不一样的放，解析结果集
        List<Tag> list = run.query(sql, new ResultSetHandler<List<Tag>>() {
            @Override
            public List<Tag> handle(ResultSet rs) throws SQLException {
                List list = new ArrayList();
                while(rs.next()) {
                    Tag tag = new Tag();

                    tag.setId(rs.getInt("id"));
                    tag.setCreated(rs.getTimestamp("created"));
                    tag.setDescription(rs.getString("description"));
                    tag.setLatestPostId(rs.getInt("latest_post_id"));
                    tag.setName(rs.getString("name"));
                    tag.setPosts(rs.getInt("posts"));
                    tag.setThumbnail(rs.getString("thumbnail"));
                    tag.setUpdated(rs.getTimestamp("updated"));

                    list.add(tag);
                }
                return list;
            }
        },params);

        return list;
    }

    /**
     *
     * @param id 查询主键
     * @return  Tag 根据主键查询的数据
     * @throws SQLException
     */
    @Override
    public Tag selectById(Integer id) throws SQLException {

        //参数列表
        Object[] params = {id};

        String sql = "select id, created, description, latest_post_id, name, posts, thumbnail, updated from mto_tag where id = ?";
        Tag tag = run.query(sql,new ResultSetHandler<Tag>() {
            @Override
            public Tag handle(ResultSet rs) throws SQLException {
                Tag tag = null;
                if(rs.next()) {
                    tag = new Tag();

                        tag.setId(rs.getInt("id"));
                        tag.setCreated(rs.getTimestamp("created"));
                        tag.setDescription(rs.getString("description"));
                        tag.setLatestPostId(rs.getInt("latest_post_id"));
                        tag.setName(rs.getString("name"));
                        tag.setPosts(rs.getInt("posts"));
                        tag.setThumbnail(rs.getString("thumbnail"));
                        tag.setUpdated(rs.getTimestamp("updated"));
                }
                return  tag;
            }
        },params);
        return tag;
    }

    /**
    *
    * @param tag 待保存的实体对象
    * @return  int 影响行数
    * @throws SQLException
    */
    @Override
    public int save(Tag tag) throws SQLException {
        StringBuilder sql = new StringBuilder();
        sql.append(" INSERT INTO mto_tag (  ");
        sql.append("  created, description, latest_post_id, name, posts, thumbnail, updated ");
        sql.append(" )  ");
        sql.append(" VALUES ");
        sql.append("   ( ");
        sql.append("       ?,  ?,  ?,  ?,  ?,  ?,  ?  ");
        sql.append("   )  ");

        Object[] params = {
            
            tag.getCreated(),  
            tag.getDescription(),  
            tag.getLatestPostId(),  
            tag.getName(),  
            tag.getPosts(),  
            tag.getThumbnail(),  
            tag.getUpdated()
        };


        int count = run.update(sql.toString(),params);

        tag.setId(super.getPK().longValue());

        return count;

    }


    /**
    * 根据主键更新
    * @param  tag
    * @return
    * @throws SQLException
    */
    @Override
    public int update(Tag tag) throws SQLException {
        StringBuilder sql = new StringBuilder();
        sql.append(" UPDATE 				");
        sql.append("   mto_tag           ");
        sql.append(" SET                    ");
            
            sql.append("   created  = ?,  ");
            sql.append("   description  = ?,  ");
            sql.append("   latest_post_id  = ?,  ");
            sql.append("   name  = ?,  ");
            sql.append("   posts  = ?,  ");
            sql.append("   thumbnail  = ?,  ");
            sql.append("   updated  = ?");

        sql.append(" WHERE id= ?            ");

        Object[] params = {
                
                tag.getCreated(), 
                tag.getDescription(), 
                tag.getLatestPostId(), 
                tag.getName(), 
                tag.getPosts(), 
                tag.getThumbnail(), 
                tag.getUpdated(), 
            tag.getId()
        };
        return run.update(sql.toString(),params);

    }

    @Override
    public int delete(Integer id) throws SQLException {
        String sql = "delete from mto_tag where id = ?";
        Object[] params = {id};
        return run.update(sql,params);
    }

}
